local bb = require("badboy")
bb.loadutilslib()
是否翻页过 = false
出战英雄列表 = {}
idxs = {}



function 六国远征关卡判断()
	local 关卡 = 0
	for i=0,40,1 do
		mSleep(125)
		--第一关已过
		x, y = findColor({184, 192, 314, 290},
			"0|0|0x0c74b2,5|-1|0x0c75b3,7|9|0x0d75b3,10|20|0x1177b3,8|24|0x1177b3,8|26|0x1176b2",
			95, 0, 0, 0)
		if x > -1 then
			if 1 > 关卡 then
				关卡= 1
			end
		end
		x, y = findColor({515, 171, 617, 271},
			"0|0|0x1477b0,8|-2|0x1477b1,15|2|0x1578b2,16|9|0x167ab1,9|19|0x1a7bb1,1|32|0x1a7bb0,14|31|0x1d7eb3",
			95, 0, 0, 0)
		if x > -1 then
			if 2 > 关卡 then
				关卡= 2
			end
		end
		
		x, y = findColor({424, 397, 530, 511},
			"0|0|0x3196cb,7|-1|0x2d93cd,-2|16|0x2a88bb,11|16|0x3293c9,12|25|0x2f8dc0,9|31|0x2f92c7,0|33|0x2a89be",
			95, 0, 0, 0)
		if x > -1 then
			if 3 > 关卡 then
				关卡=3
			end
		end
		
		x, y = findColor({691, 331, 797, 434},
			"0|0|0x3b9abf,2|-4|0x3a97be,9|-15|0x3998bf,15|-16|0x3a9dca,14|7|0x3b98b7,14|17|0x3a99bd",
			95, 0, 0, 0)
		if x > -1 then
			if 4 > 关卡 then
				关卡=4
			end
		end
		
		x, y = findColor({938, 423, 1049, 534},
			"0|0|0x1e83b1,6|0|0x1e83b1,11|0|0x2083b1,-4|9|0x2085b4,-2|15|0x1f85b3,10|16|0x1d83b3,13|27|0x197faf,0|34|0x1b81b3",
			95, 0, 0, 0)
		if x > -1 then
			if 5 > 关卡 then
				关卡=5
			end
		end
		x, y = findColor({975, 159, 1066, 270},
			"0|0|0x177cb6,-7|11|0x1980ba,-14|19|0x1e83bd,-13|29|0x157cb4,-7|33|0x1277af,5|25|0x1177b1,0|15|0x187eb6",
			95, 0, 0, 0)
		if x > -1 then
			if 6 > 关卡 then
				关卡=6
			end
		end
	end
	
	print(关卡)
	return 关卡
end

function 领取关卡奖励(关卡)
	if 关卡 == 1 then
		tap(388,151)
	elseif 关卡 == 2 then
		tap(414,275)
	elseif 关卡 == 3 then
		tap(604,518)
	elseif 关卡 == 4 then
		tap(828,472)
	elseif 关卡 == 5 then
		tap(1085,337)
	elseif 关卡 == 6 then
		tap(879,296)
	end
end

function 点击重置确定按钮()
	keepScreen(true)
	while true do
		mSleep(125)
		x, y = findColor({654, 449, 871, 537},
			"0|0|0xe3a126,11|-16|0xe5a32f,38|-15|0xe5a32f,54|1|0xffffff,53|8|0xffffff,37|19|0xe89e22,70|3|0xe69c29",
			95, 0, 0, 0)
		if x > -1 then
			tap(x,y)
			mSleep(125)
			break;
		end
	end
	keepScreen(false)
end

function 六国远征处理()
	while true do
		mSleep(125)
		--开始判断
		local 关卡 = 六国远征关卡判断()
		showHUD(id,string.format("您当前已通过第%d关",关卡),12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
		mSleep(1000)
		showHUD(id,"判断奖励是否可以领取",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
		领取关卡奖励(关卡)
		
		判断是否有确定按钮()
		--如果当前是在第六关的话，需要判断是否还有重置
		if 关卡 == 6 then
			showHUD(id,"判断当前是否可以重置关卡",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			是否可以重置 = 判断关卡是否可以重置()
			if 是否可以重置 then
				showHUD(id,"正在重置六国远征",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
				quickTap(528,668)
				mSleep(500)
				点击重置确定按钮()
				mSleep(1000)
				六国远征处理()
			end
		else
			--获取下一关卡
			关卡 = 关卡+1
			showHUD(id,string.format("您当前要进行第%d关",关卡),12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			点击要挑战的关卡(关卡)
			mSleep(1000)
			showHUD(id,"开始挑战",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			
			点击挑战按钮()
			mSleep(2000)
			showHUD(id,"删除上阵英雄",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			
			删除已上阵的英雄()
			showHUD(id,"打开英雄列表",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			
			展开英雄列表()
			showHUD(id,"分析可出战英雄",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			
			六国远征可出战英雄选择()
			
			
			showHUD(id,"开始六国远征",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			quickTap(1161,677)
			
			六国远征战斗中()
			六国远征处理()
			
		end
		
	end
end

function 六国远征战斗中()
	while true do
		mSleep(125)
		showHUD(id,"六国远征进行中",12,"0xFFFFFFFF","0x40969696",0,254,685,685,34)      --显示HUD内容
		--查找右上角自动按钮
		x, y = findColor({1167, 0, 1221, 54},
			"0|0|0x2db0d5,17|7|0x1e9ccb,32|-1|0x29afdf",
			95, 0, 0, 0)
		if x > -1 then
			print("切换至自动模式")
			quickTap(x,y)
		end
		
		--继续按钮
		x, y = findColor({539, 613, 740, 695},
			"0|0|0x377cbc,38|-8|0x3ba7d8,77|-1|0xffffff,106|-10|0xffffff,119|-10|0x37a3d4,107|6|0xffffff",
			95, 0, 0, 0)
		if x > -1 then
			print("继续按钮")
			
			quickTap(x,y)
			mSleep(3000)
			break
		end
		
		--点击屏幕继续
		x, y = findColor({538, 617, 764, 676},
			"0|0|0xffffff,8|-8|0xffffff,9|-3|0xffffff,18|2|0xffffff,40|-8|0xffffff,40|-2|0xffffff,40|6|0xffffff,69|1|0xffffff,78|1|0xffffff,69|1|0xffffff",
			95, 0, 0, 0)
		if x > -1 then
			print("点击屏幕继续")
			quickTap(x,y)
			mSleep(4000)
		end
	end
end

function 上阵英雄()
	--先隐藏HUD
	for v,s in pairs(idxs) do
		mSleep(125)
		hideHUD(s)
	end
	
	for v,s in pairs(出战英雄列表) do
		mSleep(2000)
		tap(s["x"],s["y"])
	end
end

function 加入可出战英雄(出战英雄)
	for v,s in pairs(出战英雄列表) do
		print(s)
		if s["x"] == 出战英雄["x"] and s["y"] == 出战英雄["y"] then
			break
		end
	end
	
	
end
function 分析可出战英雄()
	local 可出战英雄 = nil
	keepScreen(true)
	for i=0,20,1 do
		mSleep(125)
		point = findColors({1, 79, 1066, 706},
			"0|0|0x8ae229,0|5|0x56b211",
			95, 0, 0, 0)
		if #point ~= 0 then
			for v,s in pairs(point) do
				if 可出战英雄 == nil then
					可出战英雄 = s
					table.insert(出战英雄列表,可出战英雄)
				else
					--如果x坐标大于100，相当于换了一个英雄
					if s["x"] - 可出战英雄["x"] >= 100 then
						可出战英雄 = s
						table.insert(出战英雄列表,可出战英雄)
					end
				end
			end
		else
			--当前页可出战英雄为0
		end
	end
	for v,s in pairs(出战英雄列表) do
		mSleep(125)
		
		idx = createHUD()     --创建一个HUD
		showHUD(idx,"可出战",42,"0xffff0000","0xffffffff",0,s["x"],s["y"]-130,110,150)      --显示HUD内容
		table.insert(idxs,idx)
	end
	keepScreen(false)
end

function 展开英雄列表()
	tap(303,363)
end

function 删除已上阵的英雄()
	for i=0,10,1 do
		mSleep(125)
		x, y = findColor({1099, 48, 1249, 560},
			"0|0|0xfeffff,1|-8|0x9e0e25,0|9|0x7f0816,-10|0|0x8b0b1c,10|-1|0x8d0c1e",
			95, 0, 0, 0)
		if x > -1 then
			quickTap(x,y)
			mSleep(500)
		end
	end
end

function 点击挑战按钮()
	while true do
		mSleep(125)
		x, y = findColor({724, 601, 951, 706},
			"0|0|0xe5a127,21|-17|0xe5a52e,72|0|0xe49b26,36|17|0xe89d21,43|-8|0xffffff",
			95, 0, 0, 0)
		
		if x > -1 then
			quickTap(x,y)
			mSleep(500)
			break;
		end
	end
end

function 点击要挑战的关卡(关卡)
	if 关卡 == 1 then
		tap(244,226)
	elseif 关卡 == 2 then
		tap(571,218)
	elseif 关卡 == 3 then
		tap(469,459)
	elseif 关卡 == 4 then
		tap(730,385)
	elseif 关卡 == 5 then
		tap(992,475)
	elseif 关卡 == 6 then
		tap(1012,221)
	end
end

function 判断是否有确定按钮()
	for i=0,20,1 do
		mSleep(125)
		x, y = findColor({536, 455, 745, 540},
			"0|0|0x39a8db,39|-11|0x3db3dd,40|28|0x2f96d3,77|13|0x348ecf,57|-2|0xffffff,57|6|0xffffff",
			95, 0, 0, 0)
		if x > -1 then
			tap(x,y)
		end
	end
end

function 判断关卡是否可以重置()
	keepScreen(true)
	for i=0,20,1 do
		mSleep(125)
		x, y = findColor({361, 652, 415, 688},
			"0|0|0xbe7d15,0|7|0xbe7d15,0|10|0xbe7d15,0|13|0xbe7d15",
			95, 0, 0, 0)
		if x > -1 then
			keepScreen(false)
			
			return true
		end
	end
	
	
	keepScreen(false)
	return false
end


function  六国远征可出战英雄选择()
	print("-------")
	可出战英雄列表= {}
	体验卡何英列表={}
	战死英雄列表={}
	huds = {}
	showHUD(id,"判断战死的英雄",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
	
	for i=0,40,1 do
		mSleep(125)
		x, y = findColor({4, 82, 1071, 695},
			"0|0|0xf62b02",
			100, 0, 0, 0)
		if x > -1 then
			table.insert(战死英雄列表,{["x"]=x,["y"]=y})
			idx = createHUD()     --创建一个HUD
			showHUD(idx,"败",43,"0xff000000","0xffffffff",0,x-90,y-25,110,128)      --显示HUD内容
			table.insert(huds,idx)
		end
	end
	
	showHUD(id,"判断当前体验卡",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
	for i=0,40,1 do
		mSleep(125)
		x, y = findColor({2, 80, 1070, 700},
			"0|0|0xfdf132",
			100, 0, 0, 0)
		
		if x > -1 then
			table.insert(体验卡何英列表,{["x"]=x,["y"]=y})
			idx = createHUD()     --创建一个HUD
			showHUD(idx,"体",43,"0xffff0000","0xffffffff",0,x-14,y-82,110,128)      --显示HUD内容
			table.insert(huds,idx)
		end
	end
	
	
	showHUD(id,"判断可出战英雄",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
	for i=0,40,1 do
		mSleep(125)
		x, y = findColor({2, 80, 1070, 700},
			"0|0|0x8ae229",
			95, 0, 0, 0)
		if x > -1 then
			
			if y > 120 then
				
				table.insert(可出战英雄列表,{["x"]=x,["y"]=y})
				idx = createHUD()     --创建一个HUD
				showHUD(idx,"战",43,"0xffff0000","0xffffffff",0,x,y-118,110,128)      --显示HUD内容
			else
				idx = createHUD()     --创建一个HUD
				showHUD(idx,"略",43,"0xffff0000","0xffffffff",0,x,y-118,110,128)      --显示HUD内容
			end
			
			table.insert(huds,idx)
			
		end
	end
	
	if #可出战英雄列表 > 3 or 是否翻页过 == true then
		showHUD(id,"出战英雄",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
		local 出战数量 = 0
		
		
		if #可出战英雄列表 == 0 and 是否翻页过 == true then
			showHUD(id,"少侠已经全军覆没",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
			for v,s in pairs(huds) do
				mSleep(125)
				hideHUD(s)
			end
			可出战英雄列表= {}
			体验卡何英列表={}
			战死英雄列表={}
			mSleep(500)
			huds = {}
			tap(1164,22)
			mSleep(1500)
			tap(90,46)
			mSleep(1000)
			tap(90,46)
			mSleep(1000)
			tap(90,46)
			mSleep(1000)
			print("退出----")
			return nil
		end
		print("可出战英雄列表----")
		
		for  v,s in pairs(可出战英雄列表) do
			mSleep(500)
			tap(s["x"],s["y"])
		end
		showHUD(id,"出战完毕，准备开战",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
		for v,s in pairs(huds) do
			mSleep(125)
			hideHUD(s)
		end
		mSleep(2000)
		return  可出战英雄列表;
		
	else
		showHUD(id,"可出战英雄较少，判断是否有更多英雄",12,"0xFFFFFFFF","0x40969696",0,250,0,500,32)      --显示HUD内容
		可出战英雄列表= {}
		体验卡何英列表={}
		战死英雄列表={}
		--可出战英雄列表小于3，下拉一页
		for v,s in pairs(huds) do
			mSleep(125)
			hideHUD(s)
		end
		mSleep(500)
		huds = {}
		
		
		swip(521,661,521,100)
		touchUp(1,521,100)
		mSleep(500)
		是否翻页过 = true
		六国远征可出战英雄选择()
	end
	print("++++++++")
	
end

